package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.mapper.BaseDmaAreaMapper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.ConfigMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Objects;

/**
 * 工程管理-单条立户信息查询
 */
public class PjMeterDetail implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(), PjMeterQuery.class);
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
            ConfigMapper configMapper = factory.getMapper(ConfigMapper.class);
            PubAdminAreaMapper pubAdminAreaMapper = factory.getMapper(PubAdminAreaMapper.class,"_default");

            // 1. 查询立户数据
            PjMeter pjMeter = pjMeterMapper.getSingle(pjMeterQuery);
            // 2. 查询客户数据
            CtmInfoBean ctmInfoBean = ctmInfoMapper.getCtmInfoById(pjMeter.getUserCtmTempId(),CtmInfoBean.CTMTEMP);
            // 2.1 clientAddr->clientAddrId;clientAddr转中文
            String clientAddr = ctmInfoBean.getClientAddr();
            if(!StringUtils.isEmpty(clientAddr)) {
                ctmInfoBean.setClientAddrId(clientAddr);
                String clientAddrName = pubAdminAreaMapper.selectFullNameById(clientAddr);
                ctmInfoBean.setClientAddr(clientAddrName);
            }
            
            pjMeter.setCtmInfo(ctmInfoBean);
            // 3. 查询账户数据
            AccountInfoBean accountInfoBean = accountInfoMapper.getAccountInfoById(pjMeter.getUserAccountTempId(),AccountInfoBean.ACCOUNTTEMP);
            pjMeter.setAccountInfo(accountInfoBean);
            // 4. 查询用户信息
            UserBean userBean = userInfoMapper.getUserInfoById(pjMeter.getUserInfoTempId(),UserBean.USERTEMP);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            PjInfoBean pjInfo = pjInfoMapper.selectById(pjMeter.getConsultId());
            if (StringUtils.isEmpty(userBean.getBusinessArea())) {
            	userBean.setBusinessArea(pjInfo.getBusinessArea());
            }
            //获取水表绩效类型
            if (StringUtils.isNotEmpty(userBean.getMeterPerformanceType())) {
                userBean.setMeterPerformanceTypeName(Tools.getDicNameByCodeAndValue(factory, "MPT", userBean.getMeterPerformanceType()));
            }
            //获取DMA分区
            if (StringUtils.isNotEmpty(userBean.getDmaArea())) {
                BaseDmaAreaMapper dmaAreaMapper = factory.getMapper(BaseDmaAreaMapper.class);
                String dmaAreaName = dmaAreaMapper.findBaseDmaAreaById(userBean.getDmaArea());
                userBean.setDmaAreaName(dmaAreaName);
            }
            // 4.1 获取营业区域名称
            if (StringUtils.isNotEmpty(userBean.getBusinessArea())) {
                BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
                userBean.setBusinessAreaName(
                        businessAreaMapper.findBusinessAreaById(userBean.getBusinessArea(), userInfo.getCurWaterCode()));
            }
            // 4.2 clientContractAddr->clientContractAddrId;clientContractAddr转中文
            if(StringUtils.isNotEmpty(userBean.getClientContractAddr())) {
                userBean.setClientContractAddrId(userBean.getClientContractAddr());
                String area = pubAdminAreaMapper.selectFullNameById(userBean.getClientContractAddr());
                userBean.setClientContractAddr(area);
            }
            pjMeter.setUserInfo(userBean);
            // 5. 查询水表信息
            MsInfoDto msInfoDto = msInfoMapper.getMsInfoById(pjMeter.getMsInfoId());
            if(msInfoDto != null){
                MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);
                MeterFactoryBean meterFactory =  meterFactoryMapper.selectMeterFactoryById(msInfoDto.getMeterFactory());
                if (meterFactory != null){
                    msInfoDto.setMeterFactoryName(meterFactory.getFactoryName());
                }else {
                    msInfoDto.setMeterFactoryName(msInfoDto.getMeterFactory());
                }
            }
            pjMeter.setMsInfo(msInfoDto);

            // 获取配置信息
            String CTMADDR_IF_GET_SETUPMETERADDR = configMapper.getConfigValueByName("CTMADDR_IF_GET_SETUPMETERADDR");
            // 装表地址不为空时，客户地址是否默认为装表地址
            if (Objects.equals("1", CTMADDR_IF_GET_SETUPMETERADDR) && StringUtils.isNotBlank(userBean.getSetupMeterAddr())){
                // 设置客户地址默认为装表地址
                pjMeter.getCtmInfo().setCtmAddr(userBean.getSetupMeterAddr());
            }

            // 6. 返回数据
            MessageBean<PjMeter> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"立户单条数据查询成功",PjMeter.class);
            messageBean.setData(pjMeter);
            return messageBean;
        } catch (Exception e) {
            logger.error("立户单条数据查询失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"立户单条数据查询失败",null);
        }
    }
}